var vueObje = new Vue({
    el:'#vueObj',
    data:{userInfo:oneUserInfo}
});

$(function () {

    initTextSearch("#isvName",
        basePath + 'pfUser/searchIsvList?t='+ (new Date()).getTime()+'&isvName=',
        ['isvPid','isvName'],
        {'isvPid':"服务商pid",'isvName':'服务商名称'},
        'isvName',
        'isvName',
        true,
        selectIsvValue,
        noSelectIsvValue,
        false);

    $("#mainForm").validate({
        errorElement: "label",
        errorPlacement:function(error,element) {
            error.appendTo(element.parents('div .form-group'));
        },
        rules: {
            telephone: {
                required: true,
                minlength: 3,
                maxlength: 16,
                remote: {
                    type: "post",
                    url: basePath+'pfUser/validateTelephone',
                    data: {
                        username: function() {
                            return $("#telephone").val();
                        },
                        id: function() {
                            return $("#id").val();
                        }
                    },
                    dataType: "json",
                    dataFilter: function(data, type) {
                        if (data == "true")
                            return false;
                        else
                            return true;
                    }
                }
            }
        },
        // success:function (label) {
        //     label.removeClass('label');
        //   label.addClass('glyphicon  glyphicon-ok').css('color', '#666').text('');
        // },
        messages:{
            telephone: {
                required: "<i class='fa fa-times-circle'> 请输入手机号",
                minlength: "<i class='fa fa-times-circle'> 用户名长度不能小于3个字符",
                maxlength: "<i class='fa fa-times-circle'> 用户名长度不能大于16个字符",
                remote: "<i class='fa fa-times-circle'>  手机号已存在"
            }
        },
        onkeyup:false
    });
    regValidate(["#mainForm"]);

    // new Vue({
    //     el:'#vue',
    //     data:{obj:{}}
    // })
});
var selcetIsv = null;


function selectIsvValue(data) {
    if (!data || !data.isvPid) {
        return;
    }
    $('#isvPid').val(data.isvPid);
}

function noSelectIsvValue(data) {
    $('#isvPid').val('');
}


function goBack() {
    console.log(searchStr.replace(/,/g,"&"));
    window.location.href = basePath + 'pfUser/page?t='+new Date().getTime()+"&"+searchStr.replace(/,/g,"&");
}

function doBack() {
    var list = getlableNote('roleName',true);
    var oList=[];
    if(roleListJson){
        for (var i=0;i<roleListJson.length;i++){
            oList.push(roleListJson[i]+"");
        }
    }
    console.log(list);
    console.log(oList);
    console.log(list == oList);
    if (editType != 'info' &&(!checkDataChange("#mainForm",oneUserInfo) || JSON.stringify(list) != JSON.stringify(oList))) {  //判断图片
        swal({
            title:"提示",
            text:common_no_save_msg,
            showCancelButton:true,
            showConfirmButton:true,
            confirmButtonText:"确定",
            cancelButtonText:"取消",
            animation:"slide-from-top"
        }, function() {
            goBack();
        });
        return;
    }
    goBack();
}


function clickRole() {
    var roleList=[];

    var roles=getSelectedRows('#dataTable');
    for (var cell in roles){
        roleList.push({value:roles[cell].id,text:roles[cell].roleName});
    }
    $('#selectRole').modal('hide');
    addLableByList(roleList,"roleName");
}


$('#dataTable').on("click-row.bs.table", function (e, row, $element) {
    // checkBox 的勾选
    var index = $element[0].rowIndex - 1;
    $('#dataTable').bootstrapTable('uncheckAll');
    $('#dataTable').bootstrapTable('check', index);
});


function changeuserType(e) {
    if(userType == 'super' || userType == 'sysadmin'){
        console.log($(e).val());
        if($(e).val() == 'sysadmin'){
            $('.isv').css('display','none');
        }else{
            $('.isv').css('display','block');
        }
    }else if(userType == 'isvadmin'){
        if($(e).val() == 'sysadmin' ||  $(e).val() == 'isvcomm'){
            $('.isv').css('display','none');
        }else{
            $('.isv').css('display','block');
        }
    }else if(userType == 'isvcomm'){

    }
}


function selectRole() {
        $("#roleName").val("");
        doRefresh();
        $("#selectRole").modal(true);
}


function doRefresh() {
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'pfUser/getGridDataForCheckbox?t='+new Date().getTime()
    });
}

function getParams(params) {
    var list  = getlableNote('roleName',false).join(",");
    var roleName = $.trim($("#roleName").val());
    var postData = {
        roleName:roleName,
        idListStr : list,
        pageSize : params.pageSize,
        pageNo : params.pageNumber
    };
    // postData.userType = $.trim($("#userType").val());
    // if($("#isvPid").val() != ''){
    //     postData.isvPid =$("#isvPid").val();
    // }
    return postData;
}


function doSave() {
    var list = getlableNote('roleName',true);
    if(!$("#mainForm").valid()) return;//校验不通过
    if(list.length == 0){
        return;
    }
    var data=getFormData("#mainForm");
    data.idList = list;
    if($("#pwd").val() != ''){
        data.password = $.md5($.trim($("#pwd").val())).toUpperCase();
    }

    if( (userType == 'super' || userType == 'sysadmin') && (data.userType == 'isvadmin' ||  data.userType == 'isvcomm')
        && ($("#isvPid").val() == "")){
        toastr.error("合作商户选择不符合要求");
        return;
    }
    var url = basePath + 'pfUser/addPfUser';
    if (editType != null && editType == 'edit') {
        url = basePath + 'pfUser/editPfUser';
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(data),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success(common_save_success);
                // console.log(searchStr.replace(",","&"));
                goBack();
            }else if(data&&!data.isSuccess){
                // if (data.errorCode == 'exist') {
                //     pi_error('telephone_tag', '【' + data.telephone + '】已存在');
                //     return;
                // }
                toastr.error(data.errorDesc?data.errorDesc:common_save_fail);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}